home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / packet / thenet / x1j4_src / printpwd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-15  |  2.6 KB  |  96 lines

  1. /* ***************************************************
  2.  * printpwd
  3.  * This utility extracts a password from the parameter
  4.  * file and prints it in an operator friendly way.
  5.  * ***************************************************
  6.  */
  7.  
  8. #include <stdio.h>
  9.  
  10. char match[] = "Password" ;
  11.  
  12. main( unsigned argc, char *argv[] )
  13. {
  14.     int i, j, found_count, len, skip_first;
  15.     FILE *infile;
  16.     char inbuf[128], password[128];
  17.     char *ptr;
  18.     
  19.     if( argc < 2 )
  20.     {
  21.         fprintf( stderr, "Error - Usage is :\nprintpwd filename [ header... ]\n\n");
  22.         fprintf( stderr, "This program is used to extract the password from a\n");
  23.         fprintf( stderr, "TheNet X-series parameter save file and format it into\n");
  24.         fprintf( stderr, "a table suitable for operator use.\n");
  25.         fprintf( stderr, "It sends its output to stdout, so it may be redirected\n");
  26.         fprintf( stderr, "to a file or device. Errors are sent to stderr\n");
  27.         fprintf( stderr, "Return codes > 0 correspond to errors.\n" );
  28.         exit(1);
  29.     }
  30.     if( (infile = fopen( argv[1], "r" ) ) == NULL )
  31.     {
  32.         fprintf( stderr, "Error - Cannot open file '%s' for reading\n", argv[1] );
  33.         exit(2);
  34.     }
  35.     found_count = 0;
  36.     while( !feof( infile ) && !ferror( infile ) )
  37.     {
  38.         if( fgets( inbuf, sizeof(inbuf), infile ) != NULL )
  39.         {
  40.             if( strncmp( match, inbuf, strlen( match) ) == 0 )
  41.             {
  42.                 ptr = inbuf + strlen( match );
  43.                 while( *ptr && *ptr != '=' )
  44.                     ptr++;
  45.                 if( *ptr == '=' )
  46.                     ptr++;
  47.                 while( *ptr == ' ' )
  48.                     ptr++;
  49.                 strcpy( password, ptr );
  50.                 found_count++;
  51.             }
  52.         }
  53.     }
  54.     if( ferror( infile ) )
  55.     {
  56.         fprintf( stderr, "Error in reading from input file.\n" );
  57.         exit( 3 );
  58.     }
  59.     fclose( infile );
  60.  
  61.     if( found_count == 0 )
  62.     {
  63.         fprintf( stderr, "Error - No password entry found in file.\n" );
  64.         exit( 4 );
  65.     }
  66.     if( found_count > 1 )
  67.         fprintf( stderr, "Warning - Multiple passwords in file. Last one used.\n");
  68.  
  69.     for( len = 0, i = 2; i < argc; i++ )
  70.         len += strlen( argv[i] ) + 1;
  71.     i = ( len < 80 ) ? ( 80 - len ) / 2 : 0;
  72.     while( i-- )
  73.         fputc( ' ', stdout );
  74.     for( i = 2; i < argc; i++ )
  75.         fprintf( stdout, "%s ", argv[i]);
  76.  
  77.     fputs( "\n\n\n\n\n                    x0  x1  x2  x3  x4  x5  x6  x7  x8  x9\n", stdout );
  78.     ptr = password;
  79.     skip_first = 0;
  80.     for( i=0; i<=8; i++ )
  81.     {
  82.         fprintf(stdout, "\n                 %dx ", i );
  83.         for( j=0; j<10; j++ )
  84.         {
  85.             if( skip_first && *ptr != '\0' )
  86.                 fprintf( stdout, " %c  ", *ptr++ );
  87.             else
  88.                 fputs( "    ", stdout );
  89.             skip_first = 1;
  90.         }
  91.         fputs("\n", stdout );
  92.     }
  93.     return( 0 );
  94. }
  95.  
  96.